﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;

namespace Dragon
{
    public partial class wfVisitMapTracking : System.Web.UI.Page
    {
        DataService ds = new DataService();

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<string> roles = new List<string> { "All", "ASM", "SS", "BAM" };
            //if (Session["Username"] == null || roles.Contains(Session["UserType"].ToString()) == false)
            //{
            //    Response.Redirect("~/wfLogin.aspx");
            //}

            if (!IsPostBack)
            {
                this.txtReportDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
                ds.SetData("delete from MapTracking");
            }
        }


        protected void btnTracking_Click(object sender, EventArgs e)
        {
            DataTable dt = CreateMapTracking();
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "InitMap();", true);
        }

        protected void btnFullStep_Click(object sender, EventArgs e)
        {
            DataTable dt = CreateMapTracking();
            rptMarkers.DataSource = dt;
            rptMarkers.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "ViewFullStep();", true);
        }

        private DataTable CreateMapTracking()
        {
            DateTime reportDate = new DateTime();
            string userCode;
            try
            {
                reportDate = DateTime.ParseExact(this.txtReportDate.Text, "dd/MM/yyyy", null);
                userCode = this.ddlUser.SelectedValue.ToString();
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Information", "<script>alert('Input data is not correct!');</script>");
                return null;
            }

            string sql = "exec sp_Report_Visit_Tracking @ReportDate, @UserCode";
            SqlParameter pReportDate = new SqlParameter("@ReportDate", reportDate);
            SqlParameter pUserCode = new SqlParameter("@UserCode", userCode);
            ds.SetData(sql, pReportDate, pUserCode);

            // Lay thong tin Location
            string sqlLocation = "select R.Location from Region R inner join [User] U on R.Code = U.Region where U.Username = '" + userCode + "'";
            DataTable dtLocation = ds.GetData(sqlLocation);
            string location = dtLocation.Rows[0][0].ToString();

            // Lay thong tin quang duong di chuyen
            string sqlDistance = "select sum(Moved) from MapTracking";
            DataTable dtDistance = ds.GetData(sqlDistance);
            int distance = 0;
            try
            {
                distance = Convert.ToInt32(dtDistance.Rows[0][0].ToString());
            }
            catch { }
            this.lblInfo.Text = "Location: <b>" + location + "</b>, Distance: <b>" + String.Format("{0:#,###,###.##}", distance) + "m<b>";

            //Hien thi du lieu len Grid
            this.GridView1.DataBind();

            DataTable dt = ds.GetData("select * from MapTracking");
            int sodong = dt.Rows.Count;
            if (sodong == 0)
                return null;
            for (int i = 0; i < sodong; i++)
                dt.Rows[i]["Description"] = dt.Rows[i]["Description"].ToString().Replace("#", "<br>");

            return dt;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                try
                {
                    double moved = Convert.ToDouble(e.Row.Cells[1].Text);
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        if (moved >= 1000)
                        {
                            cell.BackColor = Color.Red;
                            cell.ForeColor = Color.White;
                        }
                    }
                }
                catch
                {
                    return;
                }                
            }
        }
    }
}